package com.example.canteen;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;


public class pay_fail_ProgressDialog extends Dialog {
    private String message;
    private final boolean canCancel;
    private TextView time,textView;
    private int num;
    private OnKeyDownListener mListener;


    public pay_fail_ProgressDialog(Context context) {
        this(context,"很抱歉,请重新支付",true);
    }


    public pay_fail_ProgressDialog(Context context, String message ,boolean canCancel) {
        super(context, R.style.LoadProgressDialog);
        this.canCancel = canCancel;
        this.message = message;

    }

    public void setMessage(String message) {
        this.message = message;
        handler.sendEmptyMessage(0);
    }

    @SuppressLint("HandlerLeak")
    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                textView.setText(message);
            }
        }
    };



    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_fail_msg_dialog);
        setCanceledOnTouchOutside(canCancel);
        time = findViewById(R.id.time);
        textView = findViewById(R.id.textview);
        textView.setText(message);
        Button button = findViewById(R.id.button);
        button.setOnClickListener(v -> dismiss());

        num = 15;
        new TimeThread().start();
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        mListener.onKeyDown(keyCode,event);
        dismiss();
        return super.onKeyDown(keyCode, event);
    }

    public interface OnKeyDownListener {
        void onKeyDown(int keyCode, KeyEvent event);
    }

    //写一个设置接口监听的方法
    public void setOnKeyDownListener(OnKeyDownListener listener) {
        mListener = listener;
    }

    private class TimeThread extends Thread {
        @Override
        public void run() {
            do {
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = 1;  //消息(一个整型值)
                    mHandler.sendMessage(msg);// 每隔1秒发送一个msg给mHandler
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (true);
        }
    }

    //在主线程里面处理消息并更新UI界面
    @SuppressLint("HandlerLeak")
    private final Handler mHandler = new Handler(){
        @SuppressLint("SetTextI18n")
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                num--;
                time.setText(num + "");
                if (num == 0) {
                    dismiss();
                }
            }
        }
    };
}

